package com.corechina.apex_osswords.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.WeiXinUserUtils;
import com.corechina.apex_osswords.domain.dto.CoreEnterpriseEngineerDto;
import com.corechina.apex_osswords.domain.dto.CoreEquipmentCheckResultDto;
import com.corechina.apex_osswords.domain.dto.CoreMaintenanceEngineerDto;
import com.corechina.apex_osswords.domain.entity.*;
import com.corechina.apex_osswords.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @program: apex_osswords
 * @description: 微信项目相关访问控制类
 * @author: cc
 * @create: 2022-03-30 15:48
 **/

@Api(tags = "微信--项目相关")
@RestController
@RequestMapping("/core/weiXinProject")
public class CoreWeiXinProjectController {


    @Autowired
    private ICoreProjectService iCoreProjectService;

    @Autowired
    private ICoreWorkOrderService iCoreWorkOrderService;

    @Autowired
    private ICoreProjectUserService iCoreProjectUserService;

    @Autowired
    private ICoreEquipmentService iCoreEquipmentService;

    @Autowired
    private ICoreProjectTypeService iCoreProjectTypeService;

    @Autowired
    private ICoreEquipmentTypeService iCoreEquipmentTypeService;

    @Autowired
    private ICoreMaintenanceEngineerService iCoreMaintenanceEngineerService;

    @Autowired
    private ICorePlaceService iCorePlaceService;

    @ApiOperation("获取列表")
    @GetMapping("/getList")
    public Response getList(Integer projectType, Integer serviceId, Integer pageIndex, Integer pageSize) {
        return iCoreProjectService.getListForWeiXin(1, projectType, serviceId, pageIndex, pageSize);
    }

    @ApiOperation("获取列表--所有用户")
    @GetMapping("/getListForAll")
    public Response getListForAll(Integer projectType, Integer serviceId, Integer pageIndex, Integer pageSize) {
        return iCoreProjectService.getListForWeiXin(0, projectType, serviceId, pageIndex, pageSize);
    }

    @ApiOperation("获取详情")
    @GetMapping("/getOne")
    public Response getOne(Integer id) {
        CoreProject coreProject = iCoreProjectService.getById(id);
        if (!coreProject.getEnterpriseId().equals(WeiXinUserUtils.getEnterpriseId())) {
            return iCoreProjectService.getOneForRepairs(id);
        }
        if (iCoreProjectUserService.getCountByUserAndProject(id) > 0 || !WeiXinUserUtils.getUserType().equals(2)) {
            return iCoreProjectService.getOne(id);
        }
        return Response.ok();
    }

    @ApiOperation("根据编号获取项目详情")
    @GetMapping("/getOneBySerialNumber")
    public Response getOneBySerialNumber(String serialNumber) {
        CoreProject coreProject = iCoreProjectService.getOneBySerialNumber(serialNumber);
        if (coreProject == null) {
            return Response.error("未查找到项目");
        }
        if (!coreProject.getEnterpriseId().equals(WeiXinUserUtils.getEnterpriseId())) {
            return iCoreProjectService.getOneForRepairs(coreProject.getId());
        }
        if (iCoreProjectUserService.getCountByUserAndProject(coreProject.getId()) > 0 || !WeiXinUserUtils.getUserType().equals(2)) {
            return iCoreProjectService.getOne(coreProject.getId());
        }
        return iCoreProjectService.getOneForRepairs(coreProject.getId());
    }

    @ApiOperation("根据编号获取项目详情--未登录")
    @GetMapping("/getOneBySerialNumberForAll")
    public Response getOneBySerialNumberForAll(String serialNumber) {
        CoreProject coreProject = iCoreProjectService.getOneBySerialNumber(serialNumber);
        if (coreProject == null) {
            return Response.error("未查找到项目");
        }
        return iCoreProjectService.getOneForRepairs(coreProject.getId());

    }

    @ApiOperation("客户发起工单")
    @PostMapping("/addOrder")
    public Response addOrder(@RequestBody CoreWorkOrder coreWorkOrder) {
        CoreProject coreProject = iCoreProjectService.getById(coreWorkOrder.getProjectId());
        if (coreProject == null) {
            return Response.error("项目不存在");
        }
        if (!coreProject.getEnterpriseId().equals(WeiXinUserUtils.getEnterpriseId())) {
            return Response.error("非企业相关人员");
        }
        coreWorkOrder.setCreator(WeiXinUserUtils.getUserId());
        coreWorkOrder.setCreateTime(LocalDateTime.now());
        coreWorkOrder.setRepairId(WeiXinUserUtils.getUserId());
        coreWorkOrder.setCreateUserType(1);
        coreWorkOrder.setState(0);
        coreWorkOrder.setAuditor(WeiXinUserUtils.getUserId());
        coreWorkOrder.setOrderNo(iCoreWorkOrderService.getOrderNo());
        coreWorkOrder.setEnterpriseId(coreProject.getEnterpriseId());
        iCoreWorkOrderService.save(coreWorkOrder);
        return Response.ok();
    }


    @ApiOperation("工单列表")
    @GetMapping("/getOrderListForWeiXin")
    public Response getListForWeiXin(String state, Integer disposeResult, Integer pageIndex, Integer pageSize) {
        return iCoreWorkOrderService.getListForWeiXin(state, disposeResult, pageIndex, pageSize);
    }

    @ApiOperation("微信编辑工单")
    @PutMapping("/updateOrderByWeiXin")
    public Response updateOrderByWeiXin(@RequestBody CoreWorkOrder coreWorkOrder) {
        return iCoreWorkOrderService.updateByWeiXin(coreWorkOrder);
    }

    @ApiOperation("微信删除工单")
    @DeleteMapping("/delOrderByWeiXin")
    public Response delOrderByWeiXin(Integer id) {
        return iCoreWorkOrderService.delOrderByWeiXin(id);
    }

    @ApiOperation("工单详情")
    @GetMapping("/getOrderDetails")
    public Response getOrderDetails(Integer id) {
        return iCoreWorkOrderService.getOne(id);
    }

    @ApiOperation("处理工单")
    @PutMapping("/disposeOrder")
    public Response disposeOrder(@RequestBody CoreWorkOrder coreWorkOrder) {
        return iCoreWorkOrderService.disposeOrder(coreWorkOrder);
    }

    @ApiOperation("个人中心计数")
    @GetMapping("/getProjectCount")
    public Response getProjectCount() {
        return iCoreProjectService.getProjectCount();
    }

    @ApiOperation("添加联系人--新人")
    @PostMapping("/addClientele")
    public Response addClientele(@RequestBody CoreMaintenanceEngineerDto coreMaintenanceEngineerDto) {
        coreMaintenanceEngineerDto.setEnterpriseId(WeiXinUserUtils.getEnterpriseId());
        return iCoreMaintenanceEngineerService.add(coreMaintenanceEngineerDto);
    }

    @ApiOperation("添加联系人--已存在")
    @PostMapping("/addEnterpriseEngineer")
    public Response addEnterpriseEngineer(@RequestBody CoreEnterpriseEngineerDto coreEnterpriseEngineerDto) {
        return iCoreMaintenanceEngineerService.addEnterpriseEngineer(coreEnterpriseEngineerDto);
    }

    @ApiOperation("根据手机号查询联系人信息")
    @GetMapping("/getByTel")
    public Response getByTel(String tel, Integer serviceId, Integer engineerType) {
        return iCoreMaintenanceEngineerService.getByTel(tel, WeiXinUserUtils.getEnterpriseId(), serviceId, engineerType);
    }

    @ApiOperation("项目设备列表")
    @GetMapping("/getEquipmentForWeiXin")
    public Response getEquipmentForWeiXin(Integer projectId, Integer place, Integer serviceId, Integer type, Integer pageIndex, Integer pageSize) {
        return iCoreEquipmentService.getEquipmentForWeiXin(projectId, place, serviceId, type, pageIndex, pageSize);
    }

    @ApiOperation("项目设备数量")
    @GetMapping("/getEquipmentCount")
    public Response getEquipmentCount() {
        return iCoreProjectService.getEquipmentCount();
    }

    @ApiOperation("项目分类--微信")
    @GetMapping("/getProjectType")
    public Response getProjectType() {
        QueryWrapper<CoreProjectType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("forbidden", 0).eq("enterprise_id", WeiXinUserUtils.getEnterpriseId());
        return Response.ok(iCoreProjectTypeService.list(queryWrapper));
    }

    @ApiOperation("设备分类--微信")
    @GetMapping("/getEquipmentType")
    public Response getEquipmentType() {
        QueryWrapper<CoreEquipmentType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id", WeiXinUserUtils.getEnterpriseId());
        return Response.ok(iCoreEquipmentTypeService.list(queryWrapper));
    }

    @ApiOperation("微信设备详情")
    @GetMapping("/getEquipmentDetailsForWeiXin")
    public Response getEquipmentDetailsForWeiXin(Integer id) {
        return iCoreEquipmentService.getOneForWeiXin(id);
    }

    @ApiOperation("根据编号获取设备详情")
    @GetMapping("/getEqmDetailsBySerialNumber")
    public Response getEquipmentDetailsBySerialNumber(String serialNumber) {
        return iCoreEquipmentService.getEquipmentDetailsBySerialNumber(serialNumber, 1);
    }

    @ApiOperation("根据编号获取设备详情--未登录")
    @GetMapping("/getEqmDetailsBySerialNumberForAll")
    public Response getEqmDetailsBySerialNumberForAll(String serialNumber) {
        return iCoreEquipmentService.getEquipmentDetailsBySerialNumber(serialNumber, 0);
    }

    @ApiOperation("添加设备")
    @PostMapping("/addEquipment")
    public Response addEquipment(@RequestBody CoreEquipment coreEquipment) {
        return iCoreEquipmentService.add(coreEquipment);
    }

    @ApiOperation("获取地点列表")
    @GetMapping("/getPlaceList")
    public Response getPlaceList(Integer projectId, Integer pageIndex, Integer pageSize) {
        return iCorePlaceService.getPlaceList(0, projectId, pageIndex, pageSize);
    }

    @ApiModelProperty("删除设备")
    @DeleteMapping("/delEquipmentBySerialNumber")
    public Response delEquipmentBySerialNumber(String serialNumber) {
        return iCoreEquipmentService.delBySerialNumber(serialNumber);
    }

    @ApiOperation("编辑设备")
    @PutMapping("/updateEquipment")
    public Response updateEquipment(@RequestBody CoreEquipment coreEquipment) {
        return iCoreEquipmentService.update(coreEquipment);
    }

    @ApiOperation("验收")
    @PutMapping("/auditOder")
    public Response auditOder(@RequestBody CoreWorkOrder coreWorkOrder) {
        return iCoreWorkOrderService.auditOder(coreWorkOrder);
    }



}
